<template>
  <!-- 接诊 -->
  <section class="jie-zhen">
    <div class="form-tool p-sm">
      <div style="line-height:36px;">
        <el-link :underline="false" @click="isShowLeft = !isShowLeft">
          <i class="iconfont icongengduo" style="font-size:25px;"></i>
        </el-link>
      </div>
      <div>
        <el-button @click="$router.push('doctorMenZhen')">
          <i class="el-icon-arrow-left"></i>返回
        </el-button>
        <el-button @click="dialogPreviewFee = true">预览费用</el-button>
        <el-button type="danger" @click="dialogGroup = true"
          >选择套餐</el-button
        >
        <!-- <el-button type="warning" @click="dialogBigTemplate = true"
          >选择模板</el-button
        > -->
        <el-button type="primary" @click="onBeforeEnd">
          <i class="iconfont iconguanbi vertical-middle"></i>
          <span class="vertical-middle">结束就诊</span>
        </el-button>
        <dialogEnd
          v-model="dialogEnd"
          :cureId="cureId"
          @cureEnd="onCureEnd"
          :doctorId="doctorId"
          :deptId="deptId"
        ></dialogEnd>
        <dialogPreviewFee
          v-model="dialogPreviewFee"
          :cureId="cureId"
        ></dialogPreviewFee>
        <dialogBigTemplate
          v-model="dialogBigTemplate"
          :doctorId="doctorId"
          :deptId="deptId"
        ></dialogBigTemplate>
        <!-- 老版套餐-->
        <!-- <dialogGroupObj v-model="dialogGroup" :patientId="patientId"></dialogGroupObj> -->
        <!-- 新版 -->
        <groupDrawer
          v-model="dialogGroup"
          :patientId="patientId"
          :deptId="deptId"
          :doctorId="doctorId"
          :cureId="cureId"
        ></groupDrawer>
      </div>
    </div>
    <div class="jie-zhen-nr">
      <div
        class="left-box p-sm"
        :style="{ width: isShowLeft ? 'auto' : '100px' }"
      >
        <patientBasicInfo
          ref="patientBasicInfo"
          :patientId="patientId"
          :cureId="cureId"
          @init="onPatientInit"
        >
          <el-button @click="dialogHistoryCase = true">历史病历</el-button
          >&nbsp;
          <!-- <el-button @click="dialogWriteData=true">完善信息</el-button> -->
        </patientBasicInfo>
        <dialogWriteData
          v-model="dialogWriteData"
          :patientId="patientId"
          :noRegister="true"
          @success="onInfo"
        ></dialogWriteData>
        <dialogHistoryCase
          v-model="dialogHistoryCase"
          :patientInfo="patientInfo"
          :print="true"
        ></dialogHistoryCase>
      </div>
      <div id="right-box" class="right-box">
        <div class="mz-box">
          <el-tabs :value="curTabIndex" type="card" @tab-click="onWatchTab">
            <el-tab-pane name="t0">
              <div slot="label" style=" position: relative;">
                <span>病例</span>
                <div v-if="templateSave.t0" class="yuan-box"></div>
              </div>
              <bingLi ref="bingLi" v-if="showTab.t0"></bingLi>
            </el-tab-pane>
            <el-tab-pane name="t1">
              <div slot="label" style=" position: relative;">
                <span>处方</span>
                <div v-if="templateSave.t1" class="yuan-box"></div>
              </div>
              <chuFang v-if="showTab.t1"></chuFang>
            </el-tab-pane>
            <el-tab-pane name="t2">
              <div slot="label" style=" position: relative;">
                <span>治疗</span>
                <div v-if="templateSave.t2" class="yuan-box"></div>
              </div>
              <cureTab v-if="showTab.t2"></cureTab>
            </el-tab-pane>
            <el-tab-pane name="t3">
              <div slot="label" style=" position: relative;">
                <span>实验室检查</span>
                <div v-if="templateSave.t3" class="yuan-box"></div>
              </div>
              <laboratory v-if="showTab.t3"></laboratory>
            </el-tab-pane>
            <el-tab-pane name="t4">
              <div slot="label" style=" position: relative;">
                <span>辅助检查</span>
                <div v-if="templateSave.t4" class="yuan-box"></div>
              </div>
              <assistCheck v-if="showTab.t4"></assistCheck>
            </el-tab-pane>
            <el-tab-pane name="t5">
              <div slot="label" style=" position: relative;">
                <span>材料费</span>
                <div v-if="templateSave.t5" class="yuan-box"></div>
              </div>
              <materialFee v-if="showTab.t5"></materialFee>
            </el-tab-pane>
            <el-tab-pane name="t6">
              <div slot="label" style=" position: relative;">
                <span>其他收费</span>
                <div v-if="templateSave.t6" class="yuan-box"></div>
              </div>
              <otherFee v-if="showTab.t6"></otherFee>
            </el-tab-pane>
            <el-tab-pane v-if="patientInfo.age < 18" name="t7">
              <div slot="label" style=" position: relative;">
                <span>儿童体检</span>
                <div v-if="templateSave.t7" class="yuan-box"></div>
              </div>
              <boayReport
                v-if="showTab.t7"
                :detail="patientInfo"
                @isChangeValue="onChangeET"
                @submit="onSubmitET"
              ></boayReport>
            </el-tab-pane>
            <el-tab-pane label="报告" name="t8">
              <reportRecord
                v-if="showTab.t8"
                :patientId="patientId"
                :cureId="cureId"
              ></reportRecord>
            </el-tab-pane>
          </el-tabs>
        </div>
      </div>
    </div>
  </section>
</template>
<script>
// import { mapState } from "vuex";
export default {
  components: {
    bingLi: () => import("./_jz/newBL.vue"),
    dialogWriteData: () => import("@/pages/_com/dialogWriteData"),
    chuFang: () => import("./_jz/prescription.vue"),
    dialogHistoryCase: () => import("./_com/dialogHistoryCase.vue"),
    cureTab: () => import("./_jz/cureTab.vue"),
    laboratory: () => import("./_jz/laboratory.vue"),
    assistCheck: () => import("./_jz/assistCheck.vue"),
    materialFee: () => import("./_jz/materialFee.vue"),
    otherFee: () => import("./_jz/otherFee.vue"),
    patientBasicInfo: () => import("@/pages/_com/patientBasicInfo.vue"),
    dialogEnd: () => import("./_jz/dialogEnd.vue"),
    dialogPreviewFee: () => import("./_com/dialogPreviewFee.vue"),
    dialogBigTemplate: () => import("./_com/dialogBigTemplate.vue"),
    boayReport: () => import("@/pages/patient/_child/childExam.vue"),
    groupDrawer: () => import("./_com/groupDrawer.vue"),
    reportRecord: () => import("@/pages/_com/reportRecord.vue"),
  },
  computed: {
    templateSave() {
      return this.$store.state.doctorMZ.templateSave;
    },
    curTabIndex() {
      return this.$store.state.doctorMZ.curTabIndex;
    },
    patientInfo() {
      return this.$store.state.doctorMZ.patientInfo;
    },
    patientId() {
      return this.$store.state.doctorMZ.patientId;
    },
    doctorId() {
      return this.$store.state.doctorMZ.doctorId;
    },
    deptId() {
      return this.$store.state.doctorMZ.deptId;
    },
    cureId() {
      return this.$store.state.doctorMZ.cureId;
    },
  },
  data() {
    return {
      dialogGroup: false, //套餐
      dialogBigTemplate: false, //大模板
      isShowLeft: true,
      dialogEnd: false,
      dialogPreviewFee: false,
      dialogWriteData: false,
      dialogHistoryCase: false,
      qxBtns: {},
      //是否加载tab内容
      showTab: {
        t0: true,
        t1: false,
        t2: false,
        t3: false,
        t4: false,
        t5: false,
        t6: false,
        t7: false,
        t8: false,
      },
    };
  },
  created() {
    let th = this;
    //=================
    th.$store.commit("Set_MzIds", {
      patientId: th.jieMi(th.$route.query.patientId),
    });
    th.$store.commit("Set_MzIds", {
      doctorId: th.jieMi(th.$route.query.doctorId),
    });
    th.$store.commit("Set_MzIds", {
      deptId: th.jieMi(th.$route.query.deptId),
    });
    th.$store.commit("Set_MzIds", {
      cureId: th.jieMi(th.$route.query.cureId),
    });
    th.showLoading();
  },
  async mounted() {
    let th = this;
    th.getCommonConfigByKey("职业").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { occupationEnum: res });
    });
    th.getCommonConfigByKey("民族").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { nationEnum: res });
    });
    th.getCommonConfigByKey("药品剂量单位").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { doseUnitEnum: res });
    });
    th.getCommonConfigByKey("药品用法").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { useEnum: res });
    });
    th.getCommonConfigByKey("药品频次").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { frequencyEnum: res });
    });
    th.getCommonConfigByKey("取药地").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { getAddressEnum: res });
    });
    th.getCommonConfigByKey("单位").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { unitEnum: res });
    });
    th.getCommonConfigByKey("检查部位").then((res) => {
      th.$store.commit("Set_doctorMzEnum", { checkPartEnum: res });
    });
    let doctor = await th.$api.getDoctorByToken();
    if (doctor.executed) {
      th.getQX();
    } else {
      th.$message.warning("检测到您不是医生");
      th.addNavMenu("工作台", "/page/index", "index");
      th.$router.push("/page/index");
    }
  },
  methods: {
    //监听儿童报告数据
    onChangeET() {
      const th = this;
      //开启修改状态
      th.$store.commit("Set_templateSave", {
        key: "t7",
        value: true,
      });
    },
    //监听儿童报告数据提交
    onSubmitET() {
      const th = this;
      th.$store.commit("Set_templateSave", {
        key: "t7",
        value: false,
      });
    },
    //更新页面患者信息
    onInfo() {
      let th = this;
      th.$refs["patientBasicInfo"].getUserInfo();
      th.$refs["bingLi"].getData();
    },
    //患者初始化完成
    onPatientInit(obj) {
      this.showLoading(false);
      this.$store.commit("Set_patientInfo", obj);
    },
    //切换tab
    onWatchTab(ref) {
      let th = this;
      th.$store.commit("Set_curTabIndex", ref.name);
      th.showTab[ref.name] = true;
    },
    //结束就诊
    onCureEnd() {
      let th = this;
      th.$router.push("/page/doctorMenZhen");
    },
    //结束之前
    onBeforeEnd() {
      const th = this;

      const fn = function(key) {
        let msg = "";
        switch (key) {
          case "t0":
            msg = "检测到病例未保存";
            break;
          case "t1":
            msg = "检测到处方未保存";
            break;
          case "t2":
            msg = "检测到治疗未保存";
            break;
          case "t3":
            msg = "检测到实验室检查未保存";
            break;
          case "t4":
            msg = "检测到辅助检查未保存";
            break;
          case "t5":
            msg = "检测到材料费未保存";
            break;
          case "t6":
            msg = "检测到其他收费未保存";
            break;
          case "t7":
            msg = "检测到报告未保存";
            break;
        }
        th.$message.warning(msg);
        // th.$confirm(msg, "提示", {
        //   confirmButtonText: "确定",
        //   cancelButtonText: "取消",
        //   type: "warning",
        //   center: true
        // })
        //   .then(() => {
        //     th.dialogEnd = true;
        //   })
        //   .catch(() => {});
      };
      let bo = false;
      for (let key in th.templateSave) {
        if (th.templateSave[key]) {
          fn(key);
          bo = true;
          break;
        }
      }
      if (!bo) {
        th.dialogEnd = true;
      }
    },
  },
  watch: {
    "$root.vw"(n) {
      let th = this;
      if (n < 1300) {
        th.isShowLeft = false;
      } else th.isShowLeft = true;
    },
  },
  beforeDestroy() {
    //这里要还原医生门诊初始数据
    const th = this;
    th.$store.commit("Set_templateData", {});
    th.$store.commit("Set_patientInfo", {});
    th.$store.commit("Set_curTabIndex", "t0");
    th.$store.commit("Clear_templateSave");
    th.$store.commit("Clear_MzEnum");
    th.$store.commit("Clear_MzIds");
  },
};
</script>
<style lang="scss" scoped>
@import "./_css/jiuZhen.scss";
</style>
